package group.igfd.manager.biz.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import group.igfd.manager.biz.entity.SyncEsListEntity;
import group.igfd.manager.biz.service.SyncEsListService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import group.igfd.manager.biz.entity.KeyWordsLibraryEntity;
import group.igfd.manager.biz.service.KeyWordsLibraryService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 关键词表（用于搜索引擎关键词联想）
 *
 * @author knight
 * @email sunlightcs@gmail.com
 * @date 2018-11-16 11:27:51
 */
@RestController
@RequestMapping("biz/keyWordsLibrary")
public class KeyWordsLibraryController {
    @Autowired
    private KeyWordsLibraryService keyWordsLibraryService;

    @Autowired
    private SyncEsListService syncEsListService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("biz:keyWordsLibrary:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = keyWordsLibraryService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("biz:keyWordsLibrary:info")
    public R info(@PathVariable("id") Integer id){
			KeyWordsLibraryEntity keyWordsLibrary = keyWordsLibraryService.selectById(id);

        return R.ok().put("keyWordsLibrary", keyWordsLibrary);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("biz:keyWordsLibrary:save")
    public R save(@RequestBody KeyWordsLibraryEntity keyWordsLibrary){
        keyWordsLibrary.setCreatedAt(new Date());
        keyWordsLibrary.setUpdatedAt(new Date());
        keyWordsLibraryService.insert(keyWordsLibrary);

        SyncEsListEntity syncEsListEntity = new SyncEsListEntity();
        syncEsListEntity.setPublishId(String.valueOf(keyWordsLibrary.getId()));
        syncEsListEntity.setType("insert");
        syncEsListEntity.setFlushType("keywordslibrary");
        syncEsListService.insert(syncEsListEntity);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("biz:keyWordsLibrary:update")
    public R update(@RequestBody KeyWordsLibraryEntity keyWordsLibrary){
        keyWordsLibraryService.updateById(keyWordsLibrary);

        SyncEsListEntity syncEsListEntity = new SyncEsListEntity();
        syncEsListEntity.setPublishId(String.valueOf(keyWordsLibrary.getId()));
        syncEsListEntity.setType("update");
        syncEsListEntity.setFlushType("keywordslibrary");
        syncEsListService.insert(syncEsListEntity);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("biz:keyWordsLibrary:delete")
    public R delete(@RequestBody Integer[] ids){
        keyWordsLibraryService.deleteBatchIds(Arrays.asList(ids));

        for (Integer id:ids) {
            SyncEsListEntity syncEsListEntity = new SyncEsListEntity();
            syncEsListEntity.setPublishId(String.valueOf(id));
            syncEsListEntity.setType("delete");
            syncEsListEntity.setFlushType("keywordslibrary");
            syncEsListService.insert(syncEsListEntity);
        }

        return R.ok();
    }

}
